/**
 * 
 */
package cu.ds.logicalsync.fytasrealm.communication.demo;

import net.jxta.peer.PeerID;
import cu.ds.logicalsync.fytasrealm.communication.CommunicationException;
import cu.ds.logicalsync.fytasrealm.communication.MessageReciever;
import cu.ds.logicalsync.fytasrealm.communication.MessageSender;
import cu.ds.logicalsync.fytasrealm.communication.Network;
import cu.ds.logicalsync.fytasrealm.communication.PeerStatusServiceProvider;
import cu.ds.logicalsync.fytasrealm.communication.event.PeerStatusEvent;
import cu.ds.logicalsync.fytasrealm.communication.event.PeerStatusListener;
import cu.ds.logicalsync.fytasrealm.communication.model.PeerInfo;
import cu.ds.logicalsync.fytasrealm.model.Map;
import cu.ds.logicalsync.fytasrealm.model.World;
import cu.ds.logicalsync.fytasrealm.util.Tools;

/**
 * ServiceClientDemo for test Service in communication.*
 * 
 * @author r.vashira
 */
public class ServiceClientDemo {
	private String clientName;
	private Network network;
	/**
	 * Optional, because it can be accessible from network.getXXXService()
	 */
	private PeerStatusServiceProvider peerStatusService;
	
	
	/**
	 * Constructor
	 */
 	public ServiceClientDemo(String clientName) {
		this.clientName = clientName;
	}
	
	/** Test Methods **/
	public void testLogin() {
		network = new Network(clientName);
		network.start();
		
		// Optional, you can access from network.getPeerStatusService directly
		peerStatusService = network.getPeerStatusService();
		
		//peerStatusService = new PeerStatusService(this.clientName);
		//peerStatusService.start();
	
		PeerInfo myInfo = peerStatusService.login(this.clientName);
		if(myInfo != null) {
			Tools.popInformationMsg("ServiceClientDemo", "PeerID return: " + myInfo.toString());
			System.out.println(myInfo.toString());
		} else {
			Tools.popErrorMsg("ServiceClientDemo", "Cannot Login, PeerInfo is NULL");
		}
	}
	
	public void testPeerStatusListener() {
		network = new Network(clientName);
		network.start();
		
		// Optional, you can access from network.getPeerStatusService directly
		peerStatusService = network.getPeerStatusService();
		
		//peerStatusService = new PeerStatusService(this.clientName);
		PeerStatusListener peerListener = new PeerStatusListener() {
			// will be called when new peer connected to the game
			public void newPeerConnected(PeerStatusEvent event) {
				PeerInfo peerInfo = event.getPeerInfo();
				String warriorName = peerInfo.getName();
				PeerID peerId = peerInfo.getId();
				
				Tools.popInformationMsg(clientName + ":" 
						+ "testPeerStatusListener", 
						"newPeerConnected: " + warriorName);
				System.out.println(peerInfo.toString());
			}
		};
		peerStatusService.addListener(peerListener);
		//peerStatusService.start();
		
		// Test by sending login to inform other peer
		peerStatusService.login(this.clientName);
		
	}
	
	public void testMessageSender() {
		network = new Network(clientName, true);
		network.start();
		
		// First login to the system
		network.getPeerStatusService().login(this.clientName);

		// Then try to send message
		MessageSender sender = new MessageSender(network.getConnection());
		sender.sendBroadCast("Hello Man");
		sender.sendComplexBroadCast();
		
	}
	
	public void testMessageReciever() {
		network = new Network(clientName, true);
		network.start();
		
		/// First login to the system
		network.getPeerStatusService().login(this.clientName);
		
		// Then stall for incoming message
		MessageReciever reciever = new MessageReciever(network.getConnection());
		reciever.listenBroadcast();
	}
	
	public void testCreateNewWorld() {
		network = new Network(clientName);
		network.start();
		
		// First login to the system
		PeerInfo myInfo = network.getPeerStatusService().login(this.clientName);
		World myWorld = null;
		
		try {
			Map map = new Map(); // TODO: get updated map from event
			network.setCurrentMap(map); // register myMap with Network
			
			myWorld = network.getMapStatusService().createNewWorld();
			// register myWorld with Network
			network.setMyWorld(myWorld);
			//myWorld.getWorldName();
			//myWorld.getMyPositionInMap();
			
			Tools.popInformationMsg("testCreateNewWorld", "Create new World for " + clientName + " successfully!");
		} catch (CommunicationException e) {
			e.printStackTrace();
			Tools.popErrorMsg("testCreateNewWorld", e.getMessage());
		}
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String clientName = args[0];
		String cmd = args[1];
		
		ServiceClientDemo client = new ServiceClientDemo(clientName);
		
		if("login".equals(cmd)) {
			client.testLogin();
		} else if("peerListener".equals(cmd)) {
			client.testPeerStatusListener();
		} else if ("msgSendB".equals(cmd)) {
			client.testMessageSender();
		} else if("msgListenB".equals(cmd)) {
			client.testMessageReciever();
		}  if("createWorld".equals(cmd)) {
			client.testCreateNewWorld();
		} else {
			System.out.println("Unrecognized command");
		}
		
	}
}
